﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewsMine.DomainObjects;
using NewsMine.Storage;
using WebApp1.Models;

namespace WebApp1.Controllers
{
    [AdminAuthorizeAttribute]
    public class AdminController : Controller
    {
       
        public ActionResult AddMenuInfo(string title,string url)
        {

            WebSiteInfo webSiteInfo = new WebSiteInfo();

            Uri uri = new Uri(url);

            webSiteInfo.Domain = uri.DnsSafeHost.ToLower();

            webSiteInfo.Name = webSiteInfo.Domain;

            webSiteInfo.HomePageUrl = "http://" + webSiteInfo.Domain + "/";


            WebSiteInfo persistedSite = (WebSiteInfo) Defaults.WebSiteInfoMasterStore.Get(webSiteInfo.Domain);
            string htmlMarkupForWebSiteInfoEditIFrame = null;
            if (persistedSite == null)
            {
                webSiteInfo.MenuItemconfigs = new List<WebSiteMenu>();
                webSiteInfo.MenuItemconfigs.Add(new WebSiteMenu {Domain=webSiteInfo.Domain,Title=title,Url=url });
                Defaults.WebSiteInfoMasterStore.Set(webSiteInfo.Domain, webSiteInfo);
                Defaults.WebSiteInfoMasterStore.Close();

                //need to allow the user to edit the website information.

                string domainEditUrl = Url.Content("~/WebSiteInfo/edit/" + webSiteInfo.Domain);
                htmlMarkupForWebSiteInfoEditIFrame = "<h1>Just now this website is inlcuded; So, please feed more details for the website info below.</h1>" +
                @"<iframe style='width:50%;height:500px;' src='" + domainEditUrl + "'/>" +
                "<h1>Home Page</h1>"+
                @"<iframe style='width:50%;height:500px;' src='" + webSiteInfo.HomePageUrl + "'/>" +
                "<h1>confirmation .....</h1>";

            }
            else
            {
                if (persistedSite.MenuItemconfigs == null)
                    persistedSite.MenuItemconfigs = new List<WebSiteMenu>();

                bool isThisMenuItemAlreadyExists = false;
                if (persistedSite.MenuItemconfigs.Count > 0)
                {
                    var existingMenuItem = (from mnuItem in persistedSite.MenuItemconfigs
                                            where mnuItem.Url.ToLower() == url.ToLower()
                                            select mnuItem).ToList();

                    if (existingMenuItem != null && existingMenuItem.Count > 0)
                    {
                        isThisMenuItemAlreadyExists = true;
                        Response.Write("Already this menu item is included, please make sure you are not adding duplicate entries");
                    }
                }

                if (isThisMenuItemAlreadyExists == false)
                {
                    persistedSite.MenuItemconfigs.Add(new WebSiteMenu { Domain = webSiteInfo.Domain, Title = title, Url = url });
                    Defaults.WebSiteInfoMasterStore.Replace(persistedSite.Domain, persistedSite);
                }
            }
            Defaults.WebSiteInfoMasterStore.Close();
            WebSiteInfo savedInfo = (WebSiteInfo)  Defaults.WebSiteInfoMasterStore.Get(webSiteInfo.Domain);

            if (savedInfo == null)
                Response.Write("<br/><br/>Due to some reason the website is not saved properly");

            Response.Write("<br/>");

            Response.Write("<h1><a href='" + savedInfo.HomePageUrl + "'>" + savedInfo.Name + "</a></h1>");

            if (savedInfo.MenuItemconfigs != null && savedInfo.MenuItemconfigs.Count > 0)
            {
                foreach (var mnuItem in savedInfo.MenuItemconfigs)
                {
                    Response.Write("<h2><a href='" + mnuItem.Url + "'>" + mnuItem.Title + "</a></h2>");
                }
            }

            if (htmlMarkupForWebSiteInfoEditIFrame != null)
            {
                Response.Write(htmlMarkupForWebSiteInfoEditIFrame);
            }

            return Content("Website info is added.");
        }

        public ActionResult AddLogoImage(string domain, string imageUrl)
        {
            return Content("Website info is added.");
        }

    }
}
